﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Numerics;

namespace RSA_Implementation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int n; 
        private int d;
        private int e;

        private  void  Encryption()
        {
            e = Convert.ToInt32(txtPublicKeyE.Text ); //257
            n = Convert.ToInt32(txtPublicKeyN.Text ); // 12702060
            byte[] messageByte = Encoding.Default.GetBytes(txtMassage.Text);
            byte[] byteCipherText = new byte[0];
            foreach(var b in messageByte)
            {
                BigInteger temp = BigInteger.ModPow(b, e, n);
                byteCipherText = appendArray(byteCipherText, temp.ToByteArray());
            }
            txtCipherText.Text = Convert.ToBase64String(byteCipherText);
        }

        private byte[] appendArray(byte[] byteCipherText, byte[] temp)
        {
            var list = new List <byte>();
            list.AddRange(byteCipherText);
            list.AddRange(temp);
            byte[] result = list.ToArray();
            return result;
        }

    /*    private  void Decryption()
        {
            d = Convert.ToInt32(txtPrivateKeyD.Text);
            n = Convert.ToInt32(txtPrivateKeyN.Text); // 12702060
            byte[] byteCipherText = Encoding.Default.GetBytes(txtCipherText.Text);
            byte[] messageByteText = new byte[0];
            foreach (var b in byteCipherText)
            {
                BigInteger temp = BigInteger.ModPow(b, d, n);
                messageByteText = appendArray(messageByteText, temp.ToByteArray());
            }
            txtMassage.Text = Convert.ToBase64String(messageByteText);
            txtMassage.Text = messageByteText.ToString();
            txtMassage.Text = Encoding.UTF8.GetString(messageByteText);


        }*/

        private void btnEncryption_Click(object sender, EventArgs e)
        {
            Encryption();
        }

        private void btnDecryption_Click(object sender, EventArgs e)
        {
          //  Decryption();
        }

    }
}
